var ProduceMaterialDataModel = function (material, type, option) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (material, type, option) {
      var fields = ['materialCode', 'materialColor', 'materialColorDescription', 'materialFactory', 'materialModel', 'materialProviderName',
        'materialName', 'materialProviderName', 'materialType'
      ];
      if (type === 'material') {
        var feedTotal, orderTotal, yltsTotal;
        if (option !== undefined) {
          fields.push('materialKlfs', 'remark');
          var mtlktTotal = material.materialMtlktTotal;
          self.mtlktTotal = Number(mtlktTotal);
          fields.pushArray(ProduceMaterialDataModel.classifyLevel);
          var difficultRate = material.materialDifficultLevelRate;
          difficultRate = Number(difficultRate);
          self.difficultRate = difficultRate;
          var feedTotalRate = material.materialFeedTotalRate;
          feedTotalRate = Number(feedTotalRate);
          self.feedTotalRate = feedTotalRate;

          var wasteRateValue = option.wasteRateValue;
          orderTotal = option.orderTotal;
          var materialFeedTotal = wasteRateValue * difficultRate + orderTotal;
          feedTotal = Math.ceil(_.round(materialFeedTotal, 4));
          yltsTotal = _.round(materialFeedTotal * feedTotalRate, 4);
        } else {
          fields.push('klccSize', 'remark');
          var orderTotal = material.orderTotal;
          orderTotal = Number(orderTotal);
          var feedTotal = material.feedTotal;
          feedTotal = Number(feedTotal);
          var yltsTotal = material.yltsTotal;
          yltsTotal = Number(yltsTotal);

          var mtlktTotal = material.mtlktTotal;
          self.mtlktTotal = Number(mtlktTotal);
          self.materialKlfs = material.klfsWay;

          var classification = material.classification;
          classifyObj = JSON.parse(classification);
          _.assign(self, classifyObj);
        }

        self.feedTotal = feedTotal;
        self.orderTotal = orderTotal;
        self.yltsTotal = yltsTotal;
        self.dataType = 'material';
      } else if (type === 'subsidiary') {
        var feedTotal, orderTotal, yltsTotal;
        if (option !== undefined) {
          fields.push('remark');
          fields.pushArray(ProduceMaterialDataModel.classifyLevel);
          var feedTotalRate = material.materialFeedTotalRate;
          feedTotalRate = Number(feedTotalRate);
          self.feedTotalRate = feedTotalRate;
          orderTotal = Math.ceil(feedTotalRate);
          feedTotal = orderTotal;
          yltsTotal = feedTotalRate;
        } else {
          fields.push('klccSize', 'remark');
          var orderTotal = material.orderTotal;
          orderTotal = Number(orderTotal);
          var feedTotal = material.feedTotal;
          feedTotal = Number(feedTotal);
          var yltsTotal = material.yltsTotal;
          yltsTotal = Number(yltsTotal);

          var classification = material.classification;
          classifyObj = JSON.parse(classification);
          _.assign(self, classifyObj);
        }
        self.feedTotal = feedTotal;
        self.orderTotal = orderTotal;
        self.yltsTotal = yltsTotal;
        self.dataType = 'subsidiary';
      } else if (type === 'glasses') {
        var feedTotal, orderTotal;
        if (option !== undefined) {
          var wasteRateValue = option.wasteRateValue;
          orderTotal = option.orderTotal;
          var difficultRate = option.difficultRate;
          var materialFeedTotal = wasteRateValue * difficultRate + orderTotal;
          feedTotal = Math.ceil(_.round(materialFeedTotal, 4));
          var yltsTotal = _.round(materialFeedTotal, 4);
        } else {
          orderTotal = material.orderTotal;
          orderTotal = Number(orderTotal);
          feedTotal = material.feedTotal;
          feedTotal = Number(feedTotal);
          yltsTotal = material.yltsTotal;
          yltsTotal = Number(yltsTotal);
        }
        self.feedTotal = feedTotal;
        self.orderTotal = orderTotal;
        self.yltsTotal = yltsTotal;
        self.dataType = 'glasses';
      }
      self.setDataFromObj(material, fields, '');
    },

    getClassifyObj: function () {
      var classifyLevel = ProduceMaterialDataModel.classifyLevel;
      var classifyObj = {};
      for (var i = 0; i < classifyLevel.length; i++) {
        var field = classifyLevel[i];
        var value = self[field];
        classifyObj[field] = value;
      }
      return classifyObj;
    }
  };

  this.getClassifyData = function (columnObj) {
    var id = columnObj.id;
    var value = self[id];
    return value;
  };

  this.getObject = function (option) {
    var postData = {};
    var fields = ['materialCode', 'materialColor', 'materialColorDescription', 'materialProviderName', 'materialFactory', 'materialModel',
      'materialName', 'materialType', 'feedTotal', 'orderTotal', 'yltsTotal'
    ];

    var dataType = this.dataType;
    if (['material', 'subsidiary'].indexOf(dataType) !== -1) {
      if (dataType === 'material') {
        fields.push('mtlktTotal', 'remark');
        postData.klfsWay = this.materialKlfs;
      } else {
        fields.push('remark');
      }

      var materialType = this.materialType;
      if (materialType === '板材圈') {
        postData.klccSize = option.ringSize;
      } else if (materialType === '板材肶') {
        postData.klccSize = option.legSize;
      } else {
        postData.klccSize = '';
      }
      var classifyObj = fn.getClassifyObj();
      postData.classification = JSON.stringify(classifyObj);
    }
    this.setPostData(postData, fields);
    return postData;
  };

  this.getMaterialSpecialRemark = function (newMaterial) {
    var materialFactory = this.materialFactory;
    if (materialFactory === '3') {
      return null;
    }
    if (newMaterial !== undefined) {
      if (this.materialColor === newMaterial.materialColor && this.feedTotal === newMaterial.feedTotal) {
        return null;
      }
    }
    return [this.materialType, materialFactory];
  };

  this.getCheckUpdateObj = function (oldMaterial) {
    if (this.materialColor !== oldMaterial.materialColor) {
      return true;
    }
    var updateObj = {};
    var fields = ['orderTotal', 'feedTotal', 'yltsTotal', 'remark'];
    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      var value = this[field];
      var oldValue = oldMaterial[field];
      updateObj[field] = value !== oldValue;
    }
    return updateObj;
  };

  fn.initData(material, type, option);
};

ProduceMaterialDataModel.getWasteRateValue = function (orderTotal, wasteRateList) {
  var wasteRate = _.find(wasteRateList, function (obj) {
    var orderTotalStart = obj.orderTotalStart;
    var orderTotalEnd = obj.orderTotalEnd;
    return orderTotal >= orderTotalStart && orderTotal <= orderTotalEnd;
  });
  if (wasteRate === undefined) {
    return 0;
  }

  var total = wasteRate.wasteTotal;
  if (!SysUtil.isEmpty(total) && total !== 0) {
    return total;
  }

  var rate = wasteRate.wasteRate;
  if (SysUtil.isEmpty(rate)) {
    return 0;
  }
  return orderTotal * rate;
};

ProduceMaterialDataModel.classifyLevel = [
  'levelRankOne',
  'levelRankTwo',
  'levelRankThree',
  'levelRankFour',
  'levelRankFive',
  'levelRankSix',
  'levelRankSeven',
  'levelRankEight',
  'levelRankNine',
  'levelRankTen'
];
